/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.osdtsystem.hierarchy;

import org.osdtsystem.utils.Storage;

/**
 * A string suffix hierarchy.
 * @author Matthias Buch-Kromann and Martin Haulrich
 */
public class SuffixHierarchy extends AbstractAffixHierarchy {
    public SuffixHierarchy(Storage dataStorage) {
        super(dataStorage);
    }

    @Override
    public AffixHClass intersectionBinary(AffixHClass hc1, AffixHClass hc2) {
        // Return empty class if either class is empty
        if (isEmpty(hc1) || isEmpty(hc2))
            return emptyClass();

        // Retrieve affixes
        String suffix1 = hc1.affix();
        String suffix2 = hc2.affix();

        // Class2 is a subclass of class1
        if (suffix2.length() >= suffix1.length() && suffix2.endsWith(suffix1))
            return hc2;


        // class1 is a subclass of class2
        if (suffix1.length() >= suffix2.length() && suffix1.endsWith(suffix2))
            return hc1;

        // Otherwise return empty class
        return emptyClass();
    }

    @Override
    public AffixHClass superclass(String suffix, int depth) {
        // Return terminal class if depth larger than affix length
        if (depth > suffix.length())
            return makeAffixClass(suffix, true);

        // Return subaffix otherwise
        return makeAffixClass(
                suffix.substring(suffix.length() - depth, suffix.length()), false);
    }
}
